1771B - Hossam and Friends - CodeForces Solution


binary search constructive algorithms dp two pointers

Please click on ads to support us..

Python Code:

def main():

    t = int(input())
    allans = []
    for _ in range(t):
        n, m = readIntArr()
        nearest_non_friend_on_left = [0] * (n + 1)
        for __ in range(m):
            a, b = readIntArr()
            if a > b:
                a, b = b, a
            nearest_non_friend_on_left[b] = max(nearest_non_friend_on_left[b], a)
        ans = 0
        curr_nearest_left = 0
        for x in range(1, n + 1):
            curr_nearest_left = max(curr_nearest_left, nearest_non_friend_on_left[x])
            ans += x - curr_nearest_left
        allans.append(ans)
    multiLineArrayPrint(allans)
    
    return

import sys
input=sys.stdin.buffer.readline  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <bits/stdc++.h>

using namespace std;

void solve()
{
	int n,m;
    cin>>n>>m;
    int near[n+1] = {0};
    for (int i=0;i<m;i++)
    {
        int a, b;
        cin>>a>>b;
        if (a>b)
        {
            if (near[b]>a) near[b]=a;
            else if (near[b]==0) near[b]=a;
        }
        else 
        {
             if (near[a]>b) near[a]=b;
            else if (near[a]==0) near[a]=b;
        }
    }
    near[n]=n+1;
    for (int i = n-1; i >0; i--)
    {
        // cout<<near[i]<<" ";
        if (near[i]==0)
        {
            near[i]=near[i+1];
        }
        else if (near[i]>near[i+1]) near[i]=near[i+1];
    }
    // cout<<endl;
    long long int sum=0;
    for (int i=1; i<n+1;i++)
    {
        sum+=near[i]-i;
    }
    cout<<sum<<endl;
    
}

int main()
{
	//need to calculate the no. of subarrays with a given XOR
	int t;
	cin>>t;
	while (t--) solve();
}


Comments

Submit
0 Comments
More Questions

144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers